import wx from "weixin-js-sdk";
import request from '@/utils/request'

//页面分享
export function getMessage(url) {
	if(url == '') {
		return {
			title: '',
			desc: '',
			imgUrl: ''  
		}
	} else if(url == '') {
		return {
			title: '',
			desc: "",
			imgUrl: ''
		}
	} 
}

//分享调用
export function openShare(){
	let that = this;
	let url = window.location.href;
	let theUrl = url.split("#")[0];
	let shareImgUrl ="https://cdn.uviewui.com/uview/swiper/swiper1.png"
	
	// return request({
	// 	url:"/wx/getAccessTokenAndJsapi?url=" + theUrl,
	// 	method: 'post'
	// }).then(res => {
	// 	_wxConfig(res) //调用后端接口，通过 config 接口注入权限验证配置
	// })
	_wxConfig(res)
}

//微信jssdk注册配置
function _wxConfig(config) {
  wx.config({
    debug: true,
    // appId: config.appId,
    // timestamp: config.timestamp,
    // nonceStr: config.nonceStr,
    // signature: config.signature,
	appId: 'wx032eaa086bf37cec',
	timestamp: 1414587457,
	nonceStr: 'Wm3WZYTPz0wzccnW' ,
	signature: '0f9de62fce790f9a083d5c99e95740ceb90c27ed',
    jsApiList: [
		'onMenuShareTimeline', 'onMenuShareAppMessage'
    ]
  })
  wx.ready(() => {
   wx.onMenuShareAppMessage({
		title: getMessage(location.href).title, // 分享标题
		desc: getMessage(location.href).desc, // 分享描述
		link: location.href, // 分享链接
		imgUrl: getMessage(location.href).imgUrl, // 必须是https网络路径,不能大于20kb
		type: '', // 分享类型,music、video或link，不填默认为link
		dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
		success: function () {}
	})		
	
	//获取“分享到朋友圈”按钮点击状态及自定义分享内容接口（即将废弃）
	wx.onMenuShareTimeline({
		title: getMessage(location.href).title, // 分享标题
		desc: getMessage(location.href).desc, // 分享描述
		link: location.href, // 分享链接
		imgUrl: getMessage(location.href).imgUrl, // 必须是https网络路径,不能大于20kb
		type: '', // 分享类型,music、video或link，不填默认为link
		dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
		success: function () {},
	})
	
})
  wx.error(res => {
    console.log("wx.config error:", res);
    // if ( config_num < 10) {
    //   config_num++
    //   _wxConfig(config)
    // }
  })
}
